// 1.引入路由并使用
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from '../store'

// 引入一级路由组件
// import Index from '../pages/Index/Index'
const Index = ()=>import('../pages/Index/Index.vue')
// import GoodsList from '../pages/GoodsList/GoodsList.vue'
const GoodsList = ()=>import('../pages/GoodsList/GoodsList.vue')
// import GoodsDetail from '../pages/GoodsDetail/GoodsDetail.vue'
const GoodsDetail = ()=>import('../pages/GoodsDetail/GoodsDetail.vue')
const Register = () =>import('../pages/Register/Register.vue')
const Login = () =>import('../pages/Login/Login.vue')
const Search = () =>import('../pages/Search/Search.vue')
const Order = () =>import('../pages/Order/Order.vue')
const AddressList = () =>import('../pages/AddressList/AddressList.vue')
const Pay = () =>import('../pages/Pay/Pay.vue')
const List = () =>import('../pages/List/List.vue')



// 引入二级路由组件
// import Store from '../views/Store/Store.vue'
const Shop = ()=>import('../views/Shop/Shop.vue')
// import Cart from '../views/Cart/Cart.vue'
const Cart = ()=>import('../views/Cart/Cart.vue')
// import Mine from '../views/Mine/Mine.vue'
const Mine = ()=>import('../views/Mine/Mine.vue')
// import Category from '../views/Category/Category.vue'
const Category = ()=>import('../views/Category/Category.vue')

// function foo(name){
//     return store.state.user.token
//    }

// 做路由和组件的配置规则
const routes = [
    {
        path:'/index',//用户访问的路径
        component:Index,//路径对应的组件
        name: '登录',
        meta: { title: '登录' },
        // 实现二级路由配置规则
        children:[
            {
                path:'shop',
                component:Shop,
                name: '商城',
                meta: { title: '商城' },
            },
            {
                path:'cart',
                component:Cart,
                name: '购物车',
                meta: { title: '购物车' },
                beforeEnter(to,from,next){
                    store.state.user? next() : next('/login')
                 }
            },
            {
                path:'mine',
                component:Mine,
                name: '我的',
                meta: { title: '我的' },
                beforeEnter(to,from,next){
                    store.state.user? next() : next('/login')
                 }
            },
            {
                path:'category',
                component:Category,
                name: '分类',
                meta: { title: '分类' },
            },
            {
                path:'/index',
                redirect:'/index/shop',
            },
            
        ],
    },
    {
        path:'/goodslist',
        component:GoodsList,
        name: '列表',
        meta: { title: '列表' },
    },
    {
        path:'/search',
        component:Search,
        name: '搜索',
        meta: { title: '搜索' },
    },
    {
        path:'/goodsdetail',
        component:GoodsDetail,
        name: '详情',
        meta: { title: '详情' },
        beforeEnter(to,from,next){
            store.state.user? next() : next('/login')
         }
    },
    {
        path:'/order',
        component:Order,
        name: '订单',
        meta: { title: '订单' },
        beforeEnter(to,from,next){
            store.state.user? next() : next('/login')
         }
    },
    {
        path:'/address',
        component:AddressList,
        name: '地址',
        meta: { title: '地址' },
        beforeEnter(to,from,next){
            store.state.user? next() : next('/login')
         }
    },
    {
        path:'/pay',
        component:Pay,
        name: '支付',
        meta: { title: '支付' },
        beforeEnter(to,from,next){
            store.state.user? next() : next('/login')
         }
    },
    {
        path:'/list',
        component:List,
        name: '商品列表',
        meta: { title: '商品列表' },
        beforeEnter(to,from,next){
            store.state.user? next() : next('/login')
         }
    },
    {
        path:'*',//表示任何不存在的路径
        redirect:'/index',
    }, 
    {
        path:'/login',
        component:Login,
        name: '登录',
        meta: { title: '登录' },
    },
    {
        path:'/register',
        component:Register,
        name: '注册',
        meta: { title: '注册' },
    },

]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

// 2.创建router对象
export default new VueRouter({
    linkActiveClass:'active',
    // 路由配置规则
    routes,
})